<?php

/*
 * To change this layout, choose Tools | Layouts
 * and open the layout in the editor.
 */

class Erra_System_FormRenderer_Native extends Erra_System_FormRenderer {

    /**
     * Erra_System_FormRenderer::_editField() - Displays an Erra_Fields_Base_Object
     *
     * @param mixed $field
     * @param mixed $actionMethod
     * @return
     */
    public static function _editField($field, $actionMethod) {
        $res = '';
        $instId = $field->isInstance() ? $field->getInstanceId() : '';
        $objectId = $field->getObjectId();

        $fieldDiv = 'FIELD_' . $field->getObjectId();

        $res = '<div id="' . $fieldDiv . '"  ' . $field->getHtmlAttributes(array('class'=>'erra-field')) . ' role="dialog">';

        $title = (!is_null($field->getDescription())) ? $field->getDescription() : $field->getObjectId();

        $helpContent = (strlen($field->getHelp())) ? ' class="live-tipsy" title="'.security_clean_input($field->getHelp()).'" ' : '';
        $res .= '<div class="erra-form-native-field_dialog_header css3legacy">';
        $res .= '<span '.$helpContent.'>' . $title . '</span>';
        $res .= '<span class="pointer erra-form-common-close_button" role="button" style="display:none;"><div class="erra-form-native-close_button">&nbsp;</div></span>';

        $res .= '</div>';

        $res .= '<div id="FIELD_' . $objectId . '_CONTENT" class="erra-form-common-dialog_content erra-form-native-dialog_content css3legacy">'; // main content

        $res .= self::startForm($field, $actionMethod, true);
        $res .= self::_editFieldRecursive($field, $actionMethod);

        $res .= '<div style="clear:both;"></div>';
        if ($field->getActionName() != null) {
            $res .= '<center><input class="erra-form-native-submit_button" name="' . $actionMethod . '" type="submit" value="' . $field->getActionName() . '"/></center>';
        }
        $res .= self::endForm($field);

        $res .= '</div>';
        $res .= '</div>';

        return $res;
    }

    /**
     * Erra_System_FormRenderer::_editFieldArray() - Displays an object array
     *
     * @param mixed $field
     * @param mixed $actionMethod
     * @return
     */
    public static function _editFieldArray($field, $actionMethod) {
        $res = '';
        $fieldName = $field->getObjectId();
        $fieldDesc = $field->getDescription();

        $array = $field->getObjectDataArray();
        $title = is_null($fieldDesc) ? $field->getObjectId() : $fieldDesc;
        $res .= '<div id="FIELD_LIST_' . $fieldName . '" data-object="'.$field->getTableName().'" '.$field->getHtmlAttributes(array('class'=>'erra-field')).' role="dialog">';

        $res .= '<div class="erra-form-native-field_dialog_header css3legacy">';
        $res .= '<span>' . $title . '</span>';
        $res .= '<span class="pointer erra-form-common-close_button" role="button"><div class="erra-form-native-close_button">&nbsp;</div></span>';

        $res .= '</div>';

        $res .= '<div id="divGridArray_' . $fieldName . '" class="erra-form-native-dialog_content css3legacy">';

        $res .= self::editRefreshHidden($field, $actionMethod);
        // add instance button, if exists
        if ($field->isCrud('C')) {
            $res .= self::editAddButton($field, $actionMethod, false);
        } else {
            $objectId = $field->getObjectId();
            $res .= '<div id = "FIELDADD_' . $objectId . '">';
            $res .= '</div>';
        }
        $res .= self::editSearchModule($field);

        $res .= (count($array) > 0) ? '<div id="divPaginatorElement_' . $fieldName . '"></div>' : '';

        if ((count($array) > 0)) {
            $aFieldPrototype = $array[0];
            $res .= self::_editTableHeader($field);
            $res .= '<tbody>';
            // Create a clone for all add/delete buttons
            $fieldClass = $field->getObjectClass();
            $aFieldClone = new $fieldClass('fieldClone', $field->getBuildArgs());
            $aFieldClone->setRenderEngine($field->renderEngine);
            $aFieldClone->setCrud($field->getCrud());
            foreach ($array as $position => $aFieldData) {
                $res .= self::_editRow($aFieldClone, $aFieldData, $position, $actionMethod);
            }
            $res .= '</tbody>';
            $res .= self::_editTableClosure();
        }
        // Custom functions for selection
        foreach ($field->getSubFields() as $aSubField) {
            if (get_class($aSubField) == 'Erra_Fields_Callback') {
                $aSubField->parent = $field;
                $res .= $aSubField->editInstance();
            }
        }
        $res .= '<div style="clear:both;"></div>';
        // Hidden form to refresh list
        $res .= self::startCallbackForm($field, 'AdminAction/refreshList');
        $res .= self::endForm($field);

        $res .= '</div>';
        $res .= '</div>';


        return $res;
    }

    /**
     * Erra_System_FormRenderer::displayMultiple() - Displays accordion field
     *
     * @param mixed $field
     * @param mixed $actionMethod
     * @return
     */
    public static function displayMultiple($field, $actionMethod) {
        $res = '';
        $instId = $field->isInstance() ? $field->getInstanceId() : '';
        $objectId = $field->getObjectId();
        $formId = 'refreshableRegion_' . $objectId;
        $res .= '<br/><div id="' . $formId . '"  >';
        $res .= '<div id="divMultiple_' . $field->getHtmlId() . '" class="erra-form-common-accordion_container">';
        
        foreach ($field->getAllInstances() as $anInstance) {
            $res .= '<div class="ui-state-default erra-form-native-multiple_field_instance css3legacy">';
            $res .= '<h3 class="erra-form-native-field_accordion_h3">';
            $res .= $field->getDescription() . ' n°' . ($anInstance->getInstanceId() + 1);
            $res .= '<span class="erra-form-common-delete_accordion_item">';
            $res .= '<span class="pointer" id="divMultipleDelete_' . $field->getHtmlId() . '"> X </span>';
            $res .= '</span>';
            $res .= '</h3>';
            $res .= '<div id="divMultipleInstance_' . $anInstance->getHtmlId() . '" class="erra-form-native-accordion_field_content">';
            $res .= self::_editFieldRecursive($anInstance, $actionMethod, $anInstance->getInstanceId());
            $res .= '<br/></div>';
            $res .= '</div>';
        }
        $res .= '</div>';
        $res .= '</div>';
        $res .= '<br/>';


        return $res;
    }

    public static function generateCss($css_path) {
        // Set defaults
        $header_background_default = "background: url('../images/native_theme/header_background.png') repeat-x scroll 0 0 #F4F4F4;";
        $section_background_default = "background: url('../images/native_theme/section_background.png') repeat-y scroll left top #EAEAEA;";
        $form_background_default = "background: url('../images/native_theme/form_background.png') repeat-y scroll left top #FAFAFA;";
        $selected_item_background_default = "background:rgb(230,230,255); opacity: 0.6;";
        $heading_font_default = "bold 1.4em arial , sans-serif";
        $heading_font_color_default = "#205A94";
        $font_default = "1em arial,sans-serif";
        $font_color_default = "#000000";
        $border_default = "1px solid #A3B5C9";
        $border_radius_default = 10;

        // Try to get theme properties from configuration
        $theme = config_get('application.forms_theme');
        $header_background = config_get($theme . '.header_background');
        $section_background = config_get($theme . '.section_background');
        $form_background = config_get($theme . '.form_background');
        $selected_item_background = config_get($theme . '.selected_item_background');
        $heading_font_color = config_get($theme . '.heading_font_color');
        $heading_font = config_get($theme . '.heading_font');
        $font_color = config_get($theme . '.font_color');
        $font = config_get($theme . '.font');
        $border = config_get($theme . '.border');
        $border_radius = config_get($theme . '.border_radius');

        // Set complete with default values if they don't exists in configuration
        $bgCss = array('background:', 'background :');
        $header_background = (strlen($header_background)) ? $header_background : $header_background_default;
        $header_background = stristrarr($header_background, $bgCss) ? $header_background : 'background:' . $header_background;

        $section_background = (strlen($section_background)) ? $section_background : $section_background_default;
        $section_background = stristrarr($section_background, $bgCss) ? $section_background : 'background:' . $section_background;

        $form_background = (strlen($form_background)) ? $form_background : $form_background_default;
        $form_background = stristrarr($form_background, $bgCss) ? $form_background : 'background:' . $form_background;

        $selected_item_background = (strlen($selected_item_background)) ? $selected_item_background : $selected_item_background_default;
        $selected_item_background = stristrarr($selected_item_background, $bgCss) ? $selected_item_background : 'background:' . $selected_item_background;

        $heading_font = (strlen($heading_font)) ? $heading_font : $heading_font_default;
        $heading_font_color = (strlen($heading_font_color)) ? $heading_font_color : 'grey';
        $font = (strlen($font)) ? $font : $font_default;
        $font_color = (strlen($font_color)) ? $font_color : $font_color_default;
        $border = (strlen($border)) ? $border : $border_default;
        $border_radius = (strlen($border_radius)) ? trim(str_replace('px', '', $border_radius)) : $border_radius_default;

        $css = '/* CSS generated by Erra_System_FormRenderer_Native class */' . "\n\n";

        $css .= '.erra-form-native-dialog_content .erra-form-native-submit_button {
    border-color: #E4E4E4 #D2D2D2 #B4B4B4;
    border-radius: 10px;
    border-style: solid;
    border-width: 1px;
    cursor: pointer;
    margin: 1em 1em 0em 1em;
    padding: 4px 17px;
    text-align: center;
    ' . $header_background . ';
    font:' . $font . ';
    color:' . $font_color . ';
}
.erra-form-native-field_dialog_header {
    font-weight:bold; 
    border-radius:' . $border_radius . 'px ' . $border_radius . 'px 0px 0px;
    padding:5px;
    text-align:center;
    font-size:1.3em;
    ' . $header_background . ';
    font:' . $heading_font . ';
    color:' . $heading_font_color . ';
    border:' . $border . ';
    border-bottom:none;
}
.erra-form-native-dialog_content {
    padding:20px;
    border:' . $border . ';
    border-top:none;
    ' . $form_background . ';
    border-radius:0px 0px ' . $border_radius . 'px ' . $border_radius . 'px;
    font:' . $font . ';
    color:' . $font_color . ';
}
.erra-form-native-multiple_field_instance {
    ' . $section_background . ';
    border-radius:' . $border_radius . 'px;
    border:' . $border . ';
}
.erra-form-native-field_accordion_h3 {
    padding:3px;
}

.erra-form-native-accordion_field_content {
    padding:10px;
    border-top:' . $border . ';
}

.erra-form-native-table td, .erra-form-native-table th {
    padding:5px;
    text-align:center;
    border-collapse:collapse;
    border:' . $border . ';
    font:' . $font . ';
    color:' . $font_color . ';
}
.erra-form-native-close_button {
    background-image:url("../images/cmsimages/close.png");
    width:15px;
    height:15px;
    float:right;
}
';
        $path = Erra_Core_File::web2path($css_path);

        file_put_contents($path, $css);
    }

}

?>
